package com.vn.rainbow.exoalarm.db.table;

import java.util.List;

import com.vn.rainbow.exoalarm.db.element.AlarmElement;
import com.vn.rainbow.exoalarm.db.element.BaseElement;
import com.vn.rainbow.exoalarm.db.settings.AlarmSettingColums;
import com.vn.rainbow.exoalarm.db.settings.DetailSettingColums;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AlarmSettingTable {
	public static final String TABLE_NAME = "configAlarm";

	public static void onCreate(SQLiteDatabase database) {
		final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
				+ AlarmSettingColums._ID + "INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ AlarmSettingColums.ACTIVE_ALARM + " INTEGER" + ");";
		database.execSQL(CREATE_TABLE);
	}

	public static void onUpgrade(SQLiteDatabase database, int oldVersion,
			int newVersion) {
		database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
	}

	public synchronized static void getAllAlarm(Context context,
			List<BaseElement> mResult) {
		final ContentResolver cr = context.getContentResolver();
		Cursor cursor = cr.query(AlarmSettingColums.CONTENT_URI, null, null,
				null, null);
		try {
			AlarmElement.parse(cursor, mResult);
			cursor.close();
		} catch (Exception e) {
			cursor.close();
		}
	}

//	public static boolean isAlarmExist(Context context, int time) {
//		final ContentResolver cr = context.getContentResolver();
//		final String[] projection = { al.TIME_ALARM };
//		Cursor cursor = cr.query(EGLockerSettingColumns.CONTENT_URI,
//				projection, EGLockerSettingColumns._ID + "=" + _id, null, null);
//		if (cursor != null) {
//			if (cursor.getCount() > 0) {
//				cursor.close();
//				return true;
//			} else {
//				cursor.close();
//			}
//		}
//		return false;
//	}

	public synchronized static AlarmElement getAlarmInfoFromId(
			Context context, String id) {
		AlarmElement info = null;
		final ContentResolver cr = context.getContentResolver();
		final String[] projection = { AlarmSettingColums._ID,
				AlarmSettingColums.ACTIVE_ALARM};
		Cursor cursor = cr.query(AlarmSettingColums.CONTENT_URI,
				projection, AlarmSettingColums._ID + "=" + id, null, null);
		try {
			cursor.moveToFirst();
			info = AlarmElement.parse(cursor);
			cursor.close();
			return info;
		} catch (Exception e) {
			// TODO: handle exception
			cursor.close();
		}
		return info;
	}
}
